package com.example.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.activity.HomeActivity;
import com.example.beans.Room;
import com.example.smart_home.R;
import com.example.util.ShowHandle;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandAdapter extends BaseExpandableListAdapter{
	
	private Context mContext;
	private String[] myFloorString = null;
	private LayoutInflater inflater = null;
	private List<List<Room>> mRoomData = null;
	private ArrayList<String[]> mFloors;
	private ShowHandle showHandle;
	private HomeActivity home;
	
	public ExpandAdapter(Context context,List<List<Room>> list){
		mContext = context;
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.showHandle = new ShowHandle(context, "it");
		this.showHandle.OpenDb();	
		mFloors = this.showHandle.getFloor();
		if(mFloors.size() == 0)
			return;
		home = (HomeActivity)context;
		myFloorString = home.floorToStringArr(mFloors);
	    mRoomData = list;
	}
	
	public void setData(List<List<Room>> list) {
        mRoomData = list;
    }

	@Override
	public Room getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mRoomData.get(groupPosition).get(childPosition);
	}
	
	
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflater.inflate(R.layout.room_item_layout, null);
		}
		RoomViewHolder roomViewHolder = new RoomViewHolder();
		/*roomViewHolder.room_image = (ImageView)convertView.findViewById(R.id.room_image);
		roomViewHolder.room_image.setBackgroundResource(getChild(groupPosition, childPosition).getBackImageview());*/
		roomViewHolder.room_name = (TextView)convertView.findViewById(R.id.room_name);
		roomViewHolder.room_name.setText(getChild(groupPosition, childPosition).getName());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mRoomData.get(groupPosition).size();
	}

	@Override
	public List<Room> getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mRoomData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mRoomData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflater.inflate(R.layout.floor_item_layout, null);
		}
		FloorViewHolder floorViewHolder = new FloorViewHolder();
		floorViewHolder.floor_name = (TextView)convertView.findViewById(R.id.floor_name);
		floorViewHolder.floor_name.setText(myFloorString[groupPosition]);
		floorViewHolder.floor_image = (ImageView)convertView.findViewById(R.id.floor_image);
		if(isExpanded){
			floorViewHolder.floor_image.setBackgroundResource(R.drawable.monitor_down);
		}else{
			floorViewHolder.floor_image.setBackgroundResource(R.drawable.monitor_go);
		}
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	private class FloorViewHolder{
		TextView floor_name;
		ImageView floor_image;
	}
	
	private class RoomViewHolder{
		ImageView room_image;
		TextView room_name;
	}

}
